package TukuduDemo;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import org.apache.log4j.Logger;

public class RestaurantDAO {
	
	private static Logger log = Logger.getLogger(RestaurantDAO.class.getName());
	private Connection connection;
	private Statement statement;
	private PreparedStatement preparedstatement;
	
	public Restaurant getRestaurant(int res_id){
		String query = "SELECT * FROM User WHERE user_id = " + res_id + ";";
		ResultSet rs = null;
		Restaurant res = null;
		
		try{
			connection = ConnectionFactory.getConnection();
			statement = connection.createStatement();
			rs = statement.executeQuery(query);
			if(rs.next()){
				res = new Restaurant();
				res.setRestaurantId(rs.getInt("restaurant_id"));
				res.setRestaurantName(rs.getString("restaurant_name"));
				res.setRestaurantUrl(rs.getString("restaurant_name"));
				res.setRestaurantDescription(rs.getString("restaurant_description"));
				res.setCuisines(rs.getString("cuisines"));
				res.setBuffet(rs.getString("buffet"));
				res.setPureVeg(rs.getString("pure_veg"));
				res.setAC(rs.getString("ac"));
				res.setBar(rs.getString("bar"));
				res.setWIFI(rs.getNString("wifi"));
				res.setLiveMusic(rs.getString("live_music"));
				res.setHomeDelivery(rs.getString("home_delivery"));
				res.setSmokingArea(rs.getString("smoking_area"));
				res.setCatering(rs.getString("catering"));
				res.setCreatedBy(rs.getInt("created_by"));
				res.setCreatedDate(rs.getDate("created_date"));
				res.setModifiedBy(rs.getInt("modified_by"));
				res.setModifiedDate(rs.getDate("modified_date"));
				res.setAddress(rs.getString("address"));
				res.setCity(rs.getString("city"));
				res.setState(rs.getString("state"));
				res.setCountry(rs.getString("country"));
				res.setZipCode(rs.getString("zip_code"));
			}
			
			System.out.println(user);
		}catch(SQLException e){
			e.printStackTrace();
			log.debug("SELECT Query Unsuccessful; Query:" + query);
			
			log.debug("Due To: " + e);
		}
		finally{
			//DbUtil.close(rs);
			//DbUtil.close(statement);
			//DbUtil.close(connection);
		}
		return user;
	}
	
	public boolean saveUser(){
		boolean val = false;
		String insertQuery = "INSERT INTO User (user_id, user_name, first_name, last_name, email, password, user_type, active_flag, created_by, created_date, modified_by, modified_date, date_of_birth, coins, no_of_businesses) VALUES(4, 'bhawana', 'Bhawana', 'Negi', 'bhawana.negi@facebook.com', 'Negative!4', 'A', 'Y', 0, '2013-04-25 12:58:08', 0, '2013-09-03 07:02:25', '1989-12-10', 1600, 90);";
	
		try{
			connection=ConnectionFactory.getConnection();
			statement= connection.createStatement();
			System.out.println(insertQuery);
			val=statement.execute(insertQuery);
			
		}catch(SQLException se){
			se.printStackTrace();
		}
		
		return val;
	}
	
	public boolean deleteUser(int id){
		boolean val=false;
		String deleteQuery = "DELETE FROM User WHERE user_id = " + id;
		try{
			connection = ConnectionFactory.getConnection();
			statement = connection.createStatement();
			System.out.println(deleteQuery);
			val = statement.execute(deleteQuery);
			System.out.println(val);
		}catch(SQLException se){
			se.printStackTrace();
		}
		return val;
	}
}


}
